package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.UiSettings;
import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.DepartmentGroupBo;
import com.topboya.linker.model.response.TencentLocationResponse;
import com.topboya.linker.ui.adapter.MapAddressAdapter;
import com.topboya.linker.ui.adapter.RecycleViewDivider;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class SelectAddressActivity extends BaseActivity  {
    private static final String TAG = "SelectAddressActivity";

    private Toolbar toolbar;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private TextureMapView texture_map_view;
    private List<TencentLocationResponse.PoisList> poisLists;
    private  TencentMap tencentMap;
    private RecyclerView rcy_address;
    private MapAddressAdapter mapAddressAdapter;
    private Button btn_save;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_select_address;
    }

    @Override
    protected void initView() {

        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);
        texture_map_view = findViewById(R.id.texture_map_view);
        rcy_address = findViewById(R.id.rcy_address);
        btn_save = findViewById(R.id.btn_save);
        tencentMap = texture_map_view.getMap();
        toolbar_title.setText("选择地址");
        tv_toolbar_right.setVisibility(View.GONE);

    }

    private void initRcyView() {

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rcy_address.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        rcy_address.setLayoutManager(manager);
        mapAddressAdapter= new MapAddressAdapter(this,poisLists);
        rcy_address.setAdapter(mapAddressAdapter);
    }

    @Override
    protected void initData() {

        poisLists = (List<TencentLocationResponse.PoisList>) getIntent().getSerializableExtra("poisLists");
         initRcyView();
        UiSettings uiSettings = tencentMap.getUiSettings();


    }

    @Override
    protected void initListener() {

        toolbar.setNavigationOnClickListener(v -> { back(); });
        btn_save.setOnClickListener(v -> { back(); });

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onBackPressed() { back(); }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    private void back() {

        Intent intent = new Intent();
        ArrayList<TencentLocationResponse.PoisList> poisLists= mapAddressAdapter.getSelectedItem();
        if (null !=poisLists && poisLists.size() > 0){

            intent.putExtra("poisList",poisLists.get(0));

        }
        setResult(1002,intent);
        finish();

        }


}

